package com.sys.web.UI;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sys.dao.DownLoadDao;
import com.sys.dao.impl.DownLoadImpl;
import com.sys.dao.impl.UploadDaoImpl;
import com.sys.domain.Download;
import com.sys.domain.Upload;
import com.sys.service.UploadOperationSer;
import com.sys.service.impl.UploadOperationImpl;

@WebServlet("/myHomeUI")
public class MyHomeUI extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取当前会话中的用户
		HttpSession session = request.getSession();
		String userId = (String) session.getAttribute("user");
		//用户为空时，不能访问“我的主页”，跳转至主页面
		if(userId==null) {
			response.sendRedirect(request.getContextPath()+"\\homeUI");
			return;
		}else if (userId.equals("admin")) {
			response.sendRedirect(request.getContextPath()+"\\adminUI");
			return;
		}
		/**
		 * 上传记录
		 */
		UploadOperationSer uOS = new UploadOperationImpl();
		List<Upload> upRecords = null;
		try {
			//获得当前用户自己上传过的文件信息
			upRecords = new UploadDaoImpl().findAllSelfUpload(userId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		//去掉文件中的用户ID
		for(Upload eUpload:upRecords) {
			eUpload.setFileId(uOS.deleteUserIdOfFile(eUpload.getFileId()));
		}
		request.setAttribute("records", upRecords);
		
		/**
		 * 下载记录
		 */
		List<Download> downRecords = null;
		DownLoadDao downLoad = new DownLoadImpl();
		try {
			System.out.println(userId);
			downRecords = downLoad.findSelfDownRecord(userId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		request.setAttribute("downRecords", downRecords);
		request.getRequestDispatcher("/WEB-INF/zuserpages/myHome.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}